package com.cyzy.utils;

import com.cyzy.pojo.Driver;
import com.cyzy.pojo.TripOrder;

/**
 * @author cuncun
 * @create 2025-06-05 上午9:37
 *
 * 计算两点之间的距离
 */
public class DistanceCalculatorUtil {

    // 地球半径（单位：米）
    private static final int R = 6371000;

    public static double calculateDistance(Driver driver, TripOrder tripOrder) {

        double lat1 = driver.getDriverX() / 1_000_000.0;
        double lon1 = driver.getDriverY() / 1_000_000.0;
        double lat2 = tripOrder.getStartLat() / 1_000_000.0;
        double lon2 = tripOrder.getStartLng() / 1_000_000.0;
        double φ1 = Math.toRadians(lat1);
        double φ2 = Math.toRadians(lat2);
        double Δφ = Math.toRadians(lat2 - lat1);
        double Δλ = Math.toRadians(lon2 - lon1);

        double a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2)
                + Math.cos(φ1) * Math.cos(φ2)
                * Math.sin(Δλ / 2) * Math.sin(Δλ / 2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return R * c; // 距离单位为米
    }
}
